package com.lavida.project.system.user.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lavida.common.utils.StringUtils;
import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.framework.web.domain.AjaxResult;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;
@Controller
@RequestMapping("/lock")
public class LockController extends BaseController{
	@Autowired
	private IUserService userService;
	@RequestMapping("")
	public String locks(ModelMap mmap,HttpServletRequest request){
		String url = request.getRequestURI();
		if(url.contains("/lock") || url.equals("") ||url==null) {
			System.out.println("刷新本页面");
		}else {
			System.out.println("需要访问的地址");
			request.getSession().setAttribute("lockurl", url);
		}
		System.out.println("访问地址:>>>>>>>>>>>>>>>>>>>>>>"+url);
    	User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
		return "page_locked";
	}
	
    @RequestMapping("/lockpass")
    @ResponseBody
   	public AjaxResult locks(String username,String password,String rememberMe){
    	Subject subject = SecurityUtils.getSubject();
    	UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        try
        {
            subject.login(token);
            return success();
        }
        catch (AuthenticationException e)
        {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            return error(msg);
        }
   	}
}
